package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.UserRetentionStatistics;
import com.example.mall.dto.UserRetentionStatisticsDto;
import com.example.mall.mapper.UserRetentionStatisticsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserRetentionStatisticsQuery;
import com.example.mall.service.IUserRetentionStatisticsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户留存率分析表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class UserRetentionStatisticsServiceImpl extends ServiceImpl<UserRetentionStatisticsMapper, UserRetentionStatistics> implements IUserRetentionStatisticsService {

    @Autowired
    private UserRetentionStatisticsMapper userRetentionStatisticsMapper;

    @Override
    public PageDTO<UserRetentionStatisticsDto> queryPage(UserRetentionStatisticsQuery userRetentionStatisticsQuery) {
        if (userRetentionStatisticsQuery == null) {
            userRetentionStatisticsQuery = new UserRetentionStatisticsQuery();
        }
        Page<UserRetentionStatistics> page = userRetentionStatisticsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserRetentionStatistics> cartPage = lambdaQuery()
                .like(userRetentionStatisticsQuery.getNewUsers()!= null, UserRetentionStatistics::getNewUsers, userRetentionStatisticsQuery.getNewUsers())
                .like(userRetentionStatisticsQuery.getRetainedUsers()!= null, UserRetentionStatistics::getRetainedUsers, userRetentionStatisticsQuery.getRetainedUsers())
                .like(userRetentionStatisticsQuery.getRetentionPeriod()!= null, UserRetentionStatistics::getRetentionPeriod, userRetentionStatisticsQuery.getRetentionPeriod())
                .like(userRetentionStatisticsQuery.getRetentionRate()!= null, UserRetentionStatistics::getRetentionRate, userRetentionStatisticsQuery.getRetentionRate())
                .like(userRetentionStatisticsQuery.getStatId()!= null, UserRetentionStatistics::getStatId, userRetentionStatisticsQuery.getStatId())
                .apply(userRetentionStatisticsQuery.getStatDate() != null,
                        "DATE_FORMAT(stat_date, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        userRetentionStatisticsQuery.getStatDate())
                .orderByDesc(UserRetentionStatistics::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, UserRetentionStatisticsDto.class);
    }

    @Override
    public UserRetentionStatistics queryUserRetentionStatisticsById(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        return userRetentionStatisticsMapper.selectById(id);
    }

    @Override
    public List<UserRetentionStatistics> queryUserRetentionStatisticsList() {
        return this.list();
    }

    @Override
    public void insert(UserRetentionStatisticsDto userRetentionStatisticsDto) {
        UserRetentionStatistics userRetentionStatistics = new UserRetentionStatistics();
        CopyUtils.copyProperties(userRetentionStatisticsDto,userRetentionStatistics);
        userRetentionStatistics.setStatId(CodeGenerateUtils.generateUniqueId());
        userRetentionStatistics.setCreateDate(new Date());
        userRetentionStatistics.setUpdateDate(new Date());
        userRetentionStatisticsMapper.insert(userRetentionStatistics);
    }

    @Override
    public void update(UserRetentionStatisticsDto userRetentionStatisticsDto) {
        AssertUtil.isNotNull(userRetentionStatisticsDto.getStatId(),"唯一标识不能为空");
        UserRetentionStatistics userRetentionStatistics = userRetentionStatisticsMapper.selectById(userRetentionStatisticsDto.getStatId());
        AssertUtil.isNotNull(userRetentionStatistics,"数据不存在");
        CopyUtils.copyProperties(userRetentionStatisticsDto,userRetentionStatistics);
        userRetentionStatistics.setUpdateDate(new Date());
        userRetentionStatisticsMapper.updateById(userRetentionStatistics);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        UserRetentionStatistics userRetentionStatistics = userRetentionStatisticsMapper.selectById(id);
        AssertUtil.isNotNull(userRetentionStatistics,"数据不存在");
        userRetentionStatisticsMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids,"唯一标识不能为空");
        List<UserRetentionStatistics> userRetentionStatistics = userRetentionStatisticsMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(userRetentionStatistics,"数据不存在");
        userRetentionStatisticsMapper.deleteBatchIds(ids);
    }
}
